##########################################################################################
# genome genome alignment Makefile



#set these
#CMD_SUBMIT?=qrsh -now n -cwd -q bc1.q,bc2.q,highmem.q -p -5
CMD_SUBMIT?=qrsh -now n -cwd -q short_jobs.q -p -5
 
#CMD_SUBMIT?=./fake_queue.pl

SEQ_BATCH_SIZE?=200
EFFECTIVE_LEN_BLAST_DB=1196146007
# set per project
#SPECIES1=homo
#SPECIES2=monodelphis
#DIR_PIPELINE_TMP=/net/cpp-group/Leo/genome_temp/monodelphis/pipeline_ds_temp
#DIR_PROJECT=/net/cpp-group/Leo/monodelphis

# shouldn't need to be changed often
DIR_BIN?=/net/cpp-group/Leo/bin
DIR_BLASTBIN?=/net/cpp-group/tools/blast


#
#    Calculated
#
DIR_SEQ?=$(DIR_PROJECT)/sequences
DIR_ALIGNRES?=$(DIR_PROJECT)/align_results
SPECIES12=$(SPECIES1)_$(SPECIES2)

#pipeline
DIR_P_SUMMARY=$(DIR_PIPELINE_TMP)/summary
DIR_P_ERRORS=$(DIR_PIPELINE_TMP)/errors
DIR_P_SEQ1=$(DIR_PIPELINE_TMP)/sequences/$(SPECIES1)
DIR_P_SEQ2=$(DIR_PIPELINE_TMP)/sequences/$(SPECIES2)
DIR_P_SEQ12=$(DIR_PIPELINE_TMP)/sequences/$(SPECIES12)

#blastpipeline
DIR_P_BLAST=$(DIR_PIPELINE_TMP)/blast
DIR_P_BLAST1=$(DIR_P_BLAST)/$(SPECIES1)
DIR_P_BLAST2=$(DIR_P_BLAST)/$(SPECIES2)
DIR_P_BLAST12=$(DIR_P_BLAST)/$(SPECIES12)
DIR_P_NEWNEW1=$(DIR_P_BLAST1)/new_new
DIR_P_OLDOLD1=$(DIR_P_BLAST1)/old_old
DIR_P_OLDNEW1=$(DIR_P_BLAST1)/new_old
DIR_P_NEWNEW2=$(DIR_P_BLAST2)/new_new
DIR_P_OLDOLD2=$(DIR_P_BLAST2)/old_old
DIR_P_OLDNEW2=$(DIR_P_BLAST2)/new_old
DIR_P_NEWNEW12=$(DIR_P_BLAST12)/new_new
DIR_P_NEW2OLD1=$(DIR_P_BLAST12)/new2_old1
DIR_P_NEW1OLD2=$(DIR_P_BLAST12)/new1_old2
DIR_P_OLD1OLD2=$(DIR_P_BLAST12)/old1_old2
DIR_P_GCNT=$(DIR_PIPELINE_TMP)/gene_counts

# for convenience
DIR_SEQ1=$(DIR_SEQ)/$(SPECIES1)
DIR_SEQ2=$(DIR_SEQ)/$(SPECIES2)
DIR_ALIGNED1=$(DIR_ALIGNRES)/$(SPECIES1)
DIR_ALIGNED2=$(DIR_ALIGNRES)/$(SPECIES2)
DIR_ALIGNED12=$(DIR_ALIGNRES)/$(SPECIES12)





.DELETE_ON_ERROR:
.PHONY: are_variables_defined first_part print_variables cleanup all
.SECONDARY :	$(DIR_P_SEQ1)/$(SPECIES1).aligned.blastall_fa \
		$(DIR_P_SEQ1)/$(SPECIES1).unaligned.blastall_fa \
		$(DIR_P_SEQ2)/$(SPECIES2).aligned.blastall_fa \
		$(DIR_P_SEQ2)/$(SPECIES2).unaligned.blastall_fa \
		$(DIR_P_SEQ1)/$(SPECIES1).aligned.blastall_fa.pin \
		$(DIR_P_SEQ1)/$(SPECIES1).unaligned.blastall_fa.pin \
		$(DIR_P_SEQ2)/$(SPECIES2).aligned.blastall_fa.pin \
		$(DIR_P_SEQ2)/$(SPECIES2).unaligned.blastall_fa.pin \
		$(DIR_ALIGNED1)/curr.blastall_results \
		$(DIR_ALIGNED2)/curr.blastall_results \
		$(DIR_ALIGNED12)/curr.blastall_results



#
#
all: do_second_part cleanup
cleanup: do_second_part
	@rm -f $(DIR_P_NEWNEW1)/*.blastall_res.aln_finished
	@rm -f $(DIR_P_NEWNEW1)/*.blastall_res             
	@rm -f $(DIR_P_NEWNEW2)/*.blastall_res.aln_finished
	@rm -f $(DIR_P_NEWNEW2)/*.blastall_res             
	@rm -f $(DIR_P_OLDNEW1)/*.blastall_res.aln_finished
	@rm -f $(DIR_P_OLDNEW1)/*.blastall_res             
	@rm -f $(DIR_P_OLDNEW2)/*.blastall_res.aln_finished
	@rm -f $(DIR_P_OLDNEW2)/*.blastall_res             
	@rm -f $(DIR_P_NEWNEW12)/*.blastall_res.aln_finished
	@rm -f $(DIR_P_NEWNEW12)/*.blastall_res             
	@rm -f $(DIR_P_NEW2OLD1)/*.blastall_res.aln_finished
	@rm -f $(DIR_P_NEW2OLD1)/*.blastall_res             
	@rm -f $(DIR_P_NEW1OLD2)/*.blastall_res.aln_finished
	@rm -f $(DIR_P_NEW1OLD2)/*.blastall_res             
	@rm -f $(DIR_P_SEQ1)/$(SPECIES1).unaligned.*.fa
	@rm -f $(DIR_P_SEQ2)/$(SPECIES2).unaligned.*.fa		


##########################################################################################
#
#   Functions


#
#	prints only last directory+file
#
#	$(call dir_file,FILE_NAME)

dir_file=$(notdir $(patsubst %/,%,$(dir $(1))))/$(notdir $(1))

#
#	create file unless already exists
#
#	$(call create_unless_exists,FILE_NAME)

create_unless_exists= @if [[ ! -f $(1) ]]; then \
echo "	$(call dir_file,$(1)) is not present. Create empty placeholder"; touch $(1); fi


#
#	blastall if both database and fasta non-zero
#
#	$(call blastall_if_non_zero,NAME,DATABASE,FA_FILE,RESULT)
blastall_if_non_zero=@if [[ -s $(2) && -s $(3) ]]; then \
	echo "	Blastall $(1) submitted"; \
	$(CMD_SUBMIT) -N "blst_$(1)" "$(DIR_BLASTBIN)/blastall -M BLOSUM80 -p blastp -d $(2) -a 2 -i $(3) -e 1e-5 -F F -b 100000 -v 100000 -a 2 -X 150 -m 7 -z $(EFFECTIVE_LEN_BLAST_DB) | $(DIR_BIN)/parse_blastall_output.pl --minimum_alignment 50 --reciprocal > $(4)" ;\
	echo "	Blastall $(1) finished"; \
else \
	rm -f $(4); \
	touch $(4); \
	echo "		Blastall $(1) skipped" ; \
fi


#
#
#
#
##########################################################################################


##########################################################################################
#
#   First Part:
#
#   	make directories
#	 	check variables defined
#		split into jobs for align
#
do_first_part:  are_variables_defined print_variables makedirectories
	@echo "- First Part"
	@echo "	Get all vs all identifier pairs..."
	@echo "	and split into jobs for align"
	@echo "	****************************************************************************"
	@$(MAKE) -f genome_align.mak first_part LEVEL_PIPE=first
	@echo
	@echo "- First Part Finished"
	@echo
	@echo

first_part: $(DIR_P_SEQ1)/$(SPECIES1).aligned.sequences_prepared_for_blastall \
            $(DIR_P_SEQ1)/$(SPECIES1).unaligned.sequences_prepared_for_blastall \
            $(DIR_P_SEQ2)/$(SPECIES2).aligned.sequences_prepared_for_blastall \
            $(DIR_P_SEQ2)/$(SPECIES2).unaligned.sequences_prepared_for_blastall


#
#
##########################################################################################







#
# create empty previous cdna lists if not exist
#
$(DIR_ALIGNED1)/curr.cdna.ids:
	@touch $@
$(DIR_ALIGNED2)/curr.cdna.ids:
	@touch $@



##########################################################################################
#
#   Make directories
#
ALL_DIR=$(DIR_P_BLAST) \
        $(DIR_ALIGNRES) \
        $(DIR_P_SUMMARY) \
        $(DIR_P_ERRORS) \
        $(DIR_P_SEQ1) \
        $(DIR_P_SEQ2) \
        $(DIR_P_NEWNEW1) \
        $(DIR_P_NEWNEW2) \
        $(DIR_P_OLDOLD1) \
        $(DIR_P_OLDOLD2) \
        $(DIR_P_OLDNEW1) \
        $(DIR_P_OLDNEW2) \
        $(DIR_P_NEWNEW12) \
        $(DIR_P_NEW2OLD1) \
        $(DIR_P_NEW1OLD2) \
        $(DIR_P_OLD1OLD2) \
        $(DIR_ALIGNED1) \
        $(DIR_ALIGNED2) \
        $(DIR_ALIGNED12) \
        $(DIR_P_SEQ12) \
        $(DIR_P_GCNT)


makedirectories: $(ALL_DIR)

$(ALL_DIR):
	@echo "	Make $@ directory"
	@mkdir -p $@
#
#
##########################################################################################








##########################################################################################
#
#   Check variables defined
#
#
are_variables_defined:
ifeq (undefined, $(origin DIR_PIPELINE_TMP))
	@echo "	DIR_PIPELINE_TMP is not undefined"; exit 255
endif
ifeq (undefined, $(origin SPECIES1))
	@echo "	SPECIES1 is not undefined"; exit 255
endif
ifeq (undefined, $(origin SPECIES2))
	@echo "	SPECIES2 is not undefined"; exit 255
endif
ifeq (undefined, $(origin DIR_PROJECT))
	@echo "	DIR_PROJECT is not undefined"; exit 255
endif
#
##########################################################################################







##########################################################################################
#
#   Print variables
#
#

print_variables:
	@echo
	@echo "	****************************************************************************"
	@echo
	@echo "Variables:"
	@echo "	SPECIES1	=$(SPECIES1)          "
	@echo "	SPECIES2	=$(SPECIES2)          "
	@echo "	DIR_PIPELINE_TMP=$(DIR_PIPELINE_TMP)  "
	@echo "	DIR_PROJECT	=$(DIR_PROJECT)          "
	@echo "	DIR_BIN		=$(DIR_BIN)           "
	@echo "	DIR_BLASTBIN	=$(DIR_BLASTBIN)      "
	@echo "	DIR_SEQ		=$(DIR_SEQ)           "
	@echo "	DIR_ALIGNRES	=$(DIR_ALIGNRES)      "
	@echo "	SPECIES12	=$(SPECIES12)      "
	@echo "	DIR_P_BLAST	=$(DIR_P_BLAST)       "
	@echo "	DIR_P_SUMMARY	=$(DIR_P_SUMMARY)     "
	@echo "	DIR_P_ERRORS	=$(DIR_P_ERRORS)      "
	@echo "	DIR_P_SEQ1	=$(DIR_P_SEQ1)        "
	@echo "	DIR_P_SEQ2	=$(DIR_P_SEQ2)        "
	@echo "	DIR_P_NEWNEW1	=$(DIR_P_NEWNEW1)     "
	@echo "	DIR_P_NEWNEW2	=$(DIR_P_NEWNEW2)     "
	@echo "	DIR_P_OLDOLD1	=$(DIR_P_OLDOLD1)     "
	@echo "	DIR_P_OLDOLD2	=$(DIR_P_OLDOLD2)     "
	@echo "	DIR_P_OLDNEW1	=$(DIR_P_OLDNEW1)     "
	@echo "	DIR_P_OLDNEW2	=$(DIR_P_OLDNEW2)     "
	@echo "	DIR_P_NEWNEW12	=$(DIR_P_NEWNEW12)    "
	@echo "	DIR_P_NEW2OLD1	=$(DIR_P_NEW2OLD1)    "
	@echo "	DIR_P_NEW1OLD2	=$(DIR_P_NEW1OLD2)    "
	@echo "	DIR_P_OLD1OLD2	=$(DIR_P_OLD1OLD2)    "
	@echo "	DIR_SEQ1	=$(DIR_SEQ1)          "
	@echo "	DIR_SEQ2	=$(DIR_SEQ2)          "
	@echo "	DIR_ALIGNED1	=$(DIR_ALIGNED1)      "
	@echo "	DIR_ALIGNED2	=$(DIR_ALIGNED2)      "
	@echo "	DIR_ALIGNED12	=$(DIR_ALIGNED12)     "
	@echo
	@echo "	****************************************************************************"
	@echo






##########################################################################################
#
#	get cdna identifiers which have / have not previously been aligned
#
#		also makes sure that file for previous results is valid by using touch
#		Since the generated ids will require prev to be rebuit anyway, this is OK
#	
$(DIR_P_SEQ1)/$(SPECIES1).unaligned.ids: $(DIR_P_SEQ1)/$(SPECIES1).aligned.ids
$(DIR_P_SEQ1)/$(SPECIES1).aligned.ids: $(DIR_ALIGNED1)/curr.cdna.ids $(DIR_SEQ1)/cdna.ids
	$(call create_unless_exists,$(DIR_ALIGNED1)/curr.blastall_results)
	$(call create_unless_exists,$(DIR_ALIGNED12)/curr.blastall_results)
	@echo "	Get aligned / unaligned identifiers for $(SPECIES1)"
	@cp $(DIR_SEQ1)/prot.mapped.fa $(DIR_P_SEQ1)/prot.fa
	@$(DIR_BIN)/diff_sets \
        --file1in $< \
        --file2in $(DIR_SEQ1)/cdna.ids \
        --counts $(DIR_P_SUMMARY)/prev_aligned_unique_sequences.$(SPECIES1).counts \
        --shared $(DIR_P_SEQ1)/$(SPECIES1).aligned.ids \
        --file2_only $(DIR_P_SEQ1)/$(SPECIES1).unaligned.ids \
        --err_log $(DIR_P_ERRORS)/identifier_diff.$(SPECIES1).errors

# bug fix removed obsolete identifiers 
# --file1_only $(DIR_P_SEQ1)/$(SPECIES1).aligned.ids \


$(DIR_P_SEQ2)/$(SPECIES2).unaligned.ids: $(DIR_P_SEQ2)/$(SPECIES2).aligned.ids
$(DIR_P_SEQ2)/$(SPECIES2).aligned.ids: $(DIR_ALIGNED2)/curr.cdna.ids $(DIR_SEQ2)/cdna.ids 
	$(call create_unless_exists,$(DIR_ALIGNED2)/curr.blastall_results)
	$(call create_unless_exists,$(DIR_ALIGNED12)/curr.blastall_results)
	@echo "	Get aligned / unaligned identifiers for $(SPECIES2)"
	@cp $(DIR_SEQ2)/prot.mapped.fa $(DIR_P_SEQ2)/prot.fa
	@$(DIR_BIN)/diff_sets \
        --file1in $< \
        --file2in $(DIR_SEQ2)/cdna.ids \
        --counts $(DIR_P_SUMMARY)/prev_aligned_unique_sequences.$(SPECIES2).counts \
        --shared $(DIR_P_SEQ2)/$(SPECIES2).aligned.ids \
        --file2_only $(DIR_P_SEQ2)/$(SPECIES2).unaligned.ids \
        --err_log $(DIR_P_ERRORS)/identifier_diff.$(SPECIES2).errors

# bug fix removed obsolete identifiers 
# --file1_only $(DIR_P_SEQ2)/$(SPECIES2).aligned.ids \


#
#
##########################################################################################




	



##########################################################################################
#
# extract matching sequences
#

# aligned

%.blastall_fa: %.ids
	@echo "	Extracting sequences from prot.fa for $(*F).ids"
	@rm -f $@
	@touch $@
	@$(DIR_BIN)/seq_extract \
        --sequences $(<D)/prot.fa \
        --names \
        --err_log $(DIR_P_ERRORS)/seq_extract.$(*F).errors \
        <  $< \
        > $@
	@echo "	Finished extracting sequences from prot.fa for $(*F).ids"
#
##########################################################################################

##########################################################################################
#
# formatdb
#
%.blastall_fa.pin: %.blastall_fa
	@echo "	blast formatdb for $(<F)."
	@$(DIR_BLASTBIN)/formatdb -i $<
	@echo "	formatdb finished for $(<F)"
#
#
##########################################################################################


##########################################################################################
#
#   break up fasta into chunks of $(SEQ_BATCH_SIZE) in preparation of align
#
%.unaligned.sequences_prepared_for_blastall: %.unaligned.blastall_fa %.unaligned.blastall_fa.pin
	@echo "	remove all previous sequences"
	@rm -f $(subst blastall_fa,*.fa,$<)
	@touch $(subst blastall_fa,0.fa,$<)
	@touch $@
	@echo "	Break $(call dir_file,$<) into chunks of $(SEQ_BATCH_SIZE)..."
	@$(DIR_BIN)/fasta_split \
        --every $(SEQ_BATCH_SIZE) \
        --path "$(subst blastall_fa,[INDEX].fa,$<)" \
        < $<
	@echo `find $(<D) -name $(notdir $(subst blastall_fa,*.fa,$(<F))) | wc -l` fasta files > $(DIR_P_SUMMARY)/blastall_split.$(*F).unaligned.file_counts

%.aligned.sequences_prepared_for_blastall: %.aligned.blastall_fa %.aligned.blastall_fa.pin
	@touch $@
#
##########################################################################################








































##########################################################################################
#
#   Second Part:
#
#   	
#	 	check variables defined
#		split into jobs for align
#
do_second_part: do_first_part
	@echo "- Second Part"
	@echo "	Align all against all..."
	@echo "	and combine results"
	@echo "	****************************************************************************"
	@echo
	@$(MAKE) -f genome_align.mak second_part LEVEL_PIPE=second
	@echo "- Second Part Finished"
	@echo
	@echo

#

second_part: $(DIR_ALIGNRES)/$(SPECIES1)/curr.blastall_results \
             $(DIR_ALIGNRES)/$(SPECIES2)/curr.blastall_results \
	     $(DIR_ALIGNED12)/curr.blastall_results
	     
#
#
##########################################################################################


# list of input fasta files
UNALIGNED1_FA=$(wildcard $(DIR_P_SEQ1)/$(SPECIES1).unaligned.*.fa)
UNALIGNED2_FA=$(wildcard $(DIR_P_SEQ2)/$(SPECIES2).unaligned.*.fa)















##########################################################################################
#
#
#	Filter and normalize data after alignment for species 1 vs 1, 1 vs 2 and 2 vs 2

#
#	Filter for 60% coverage of the longer and 75% of the shorter of each sequence pair
# 
%.filtered_blastall_results: %.concatenated_blastall_results
	@echo "	Filter all alignment results for $(call dir_file,$<) "
	@$(DIR_BIN)/seq_pairs_filter_align_results \
                --input_file $< \
                --coverage_residues 0.6 \
                --coverage_fragment 0.75 \
		> $@
	@echo "	All alignment results cover either 60% of the longer or 75% of the shorter"
	@echo





#
#	Normalise all data
#
%.normalized_blastall_results: %.filtered_blastall_results
	@echo "	Normalize alignment identifiers in alphabetical order & choose lowest e-value"
	@$(DIR_BIN)/seq_pairs_normalize_ids \
                --input_file $< \
		--results_summary $(DIR_P_SUMMARY)/blast.$(*F).normalized.counts \
                --discard_identical \
		> $@
	@echo "	All alignment identifiers in alphabetical order with lowest e-value chosen"
	@echo



#
#	Save results
#		save first to tmp file, rename old file, rename tmp file
# 
$(DIR_ALIGNRES)/%/curr.blastall_results:  $(DIR_P_BLAST)/%.normalized_blastall_results
	@echo "	Save blastall results for $(*F)"
	@# save sequences/cdna.ids to align_results/cdna.ids with original timestamp
	@if [[ -f $(DIR_SEQ)/$(*F)/cdna.ids ]]; then \
            cp -p $(DIR_SEQ)/$(*F)/cdna.ids $(@D)/curr.cdna.ids.tmp; \
            mv $(@D)/curr.cdna.ids $(@D)/cdna.ids.prev; \
            mv $(@D)/curr.cdna.ids.tmp $(@D)/curr.cdna.ids; \
        fi
	@# make sure directory is writeable
	@cp $< $@.tmp
	@# create prev file if necessary and backup to .prev
	$(call create_unless_exists,$(@D)/blastall_results.prev)
	@find $(@D) -name "*.prev" -exec mv '{}' '{}'.prev ';'
	@mv $(@D)/curr.blastall_results $(@D)/blastall_results.prev
	@# concatenate files
	@mv $@.tmp $@
	@date > $(DIR_P_SUMMARY)/blast.$(*F).blast_finish.date
	@echo "	Finished saving blastall results for $(*F)"

#
#
##########################################################################################















##########################################################################################
#
#   Species 1 blastall against itself
#
#	Unaligned vs Unaligned sequences
#
U1U1_RES_FILES= $(UNALIGNED1_FA:$(DIR_P_SEQ1)/$(SPECIES1).unaligned.%.fa=$(DIR_P_NEWNEW1)/$(SPECIES1).unaligned.%.blastall_res)
$(DIR_P_NEWNEW1)/$(SPECIES1).unaligned.%.blastall_res: \
		$(DIR_P_SEQ1)/$(SPECIES1).unaligned.%.fa \
		$(DIR_P_SEQ1)/$(SPECIES1).unaligned.blastall_fa
	$(call blastall_if_non_zero,UU1_$(*F)_$(SPECIES1),$(DIR_P_SEQ1)/$(SPECIES1).unaligned.blastall_fa,$<,$@)
	@touch $@
	@touch $@.aln_finished

U1U1_RES=$(DIR_P_NEWNEW1)/blastall_res.concat_res
$(U1U1_RES): $(U1U1_RES_FILES)
	@echo "	Concatenating blastall results for $(SPECIES1) Unaligned vs Unaligned"
	@find $(DIR_P_NEWNEW1) -name "$(SPECIES1).unaligned.*.blastall_res" | xargs cat > $@
	@touch $@
	@echo "	Finished concatenating blastall results for $(SPECIES1) Unaligned vs Unaligned"
	

#
#	Unaligned vs Aligned sequences
#
A1U1_RES_FILES= $(UNALIGNED1_FA:$(DIR_P_SEQ1)/$(SPECIES1).unaligned.%.fa=$(DIR_P_OLDNEW1)/$(SPECIES1).unaligned.%.blastall_res)
$(DIR_P_OLDNEW1)/$(SPECIES1).unaligned.%.blastall_res: \
		$(DIR_P_SEQ1)/$(SPECIES1).unaligned.%.fa \
		$(DIR_P_SEQ1)/$(SPECIES1).aligned.blastall_fa
	$(call blastall_if_non_zero,AU1_$(*F)_$(SPECIES1),$(DIR_P_SEQ1)/$(SPECIES1).aligned.blastall_fa,$<,$@)
	@touch $@
	@touch $@.aln_finished

A1U1_RES=$(DIR_P_OLDNEW1)/blastall_res.concat_res
$(A1U1_RES): $(A1U1_RES_FILES)
	@echo "	Concatenating blastall results for $(SPECIES1) Unaligned vs Aligned"
	@find $(DIR_P_OLDNEW1) -name "$(SPECIES1).unaligned.*.blastall_res" | xargs cat > $@
	@touch $@
	@echo "	Finished concatenating blastall results for $(SPECIES1) Unaligned vs Aligned"

#
#	Aligned vs Aligned old results
#
A1A1_RES=$(DIR_P_OLDOLD1)/matching_from_prev.blastall_res
$(A1A1_RES): $(DIR_P_SEQ1)/$(SPECIES1).aligned.ids
	@echo "	Filtering previous blastall results matching current $(SPECIES1)" 
	@$(DIR_BIN)/seq_pairs_filter_by_identifier \
        --matching_identifiers $< \
        --input_file $(DIR_ALIGNED1)/curr.blastall_results \
        --results_summary $(DIR_P_SUMMARY)/prev_blastall_results.$(SPECIES1).counts \
        --both $@
	@echo "	Finished filtering previous blastall results matching current $(SPECIES1)" 

#
#	Concatenate results together
# 
$(DIR_P_BLAST)/$(SPECIES1).concatenated_blastall_results: $(A1A1_RES) $(U1U1_RES) $(A1U1_RES)
	@echo "	Concatenating blastall results for $(SPECIES2) against itself"
	@cat $^ > $@
	@echo "	Finished concatenating blastall results for $(SPECIES2) against itself"

#
##########################################################################################






##########################################################################################
#
#   Species 2 blastall against itself
#
#	Unaligned vs Unaligned sequences
#
U2U2_RES_FILES= $(UNALIGNED2_FA:$(DIR_P_SEQ2)/$(SPECIES2).unaligned.%.fa=$(DIR_P_NEWNEW2)/$(SPECIES2).unaligned.%.blastall_res)
$(DIR_P_NEWNEW2)/$(SPECIES2).unaligned.%.blastall_res: \
		$(DIR_P_SEQ2)/$(SPECIES2).unaligned.%.fa \
		$(DIR_P_SEQ2)/$(SPECIES2).unaligned.blastall_fa
	$(call blastall_if_non_zero,UU2_$(*F)_$(SPECIES2),$(DIR_P_SEQ2)/$(SPECIES2).unaligned.blastall_fa,$<,$@)
	@touch $@
	@touch $@.aln_finished

U2U2_RES=$(DIR_P_NEWNEW2)/blastall_res.concat_res
$(U2U2_RES): $(U2U2_RES_FILES)
	@echo "	Concatenating blastall results for $(SPECIES2) Unaligned vs Unaligned"
	@find $(DIR_P_NEWNEW2) -name "$(SPECIES2).unaligned.*.blastall_res" | xargs cat > $@
	@touch $@
	@echo "	Finished concatenating blastall results for $(SPECIES2) Unaligned vs Unaligned"
	

#
#	Unaligned vs Aligned sequences
#
A2U2_RES_FILES= $(UNALIGNED2_FA:$(DIR_P_SEQ2)/$(SPECIES2).unaligned.%.fa=$(DIR_P_OLDNEW2)/$(SPECIES2).unaligned.%.blastall_res)
$(DIR_P_OLDNEW2)/$(SPECIES2).unaligned.%.blastall_res: \
		$(DIR_P_SEQ2)/$(SPECIES2).unaligned.%.fa \
		$(DIR_P_SEQ2)/$(SPECIES2).aligned.blastall_fa
	$(call blastall_if_non_zero,AU2_$(*F)_$(SPECIES2),$(DIR_P_SEQ2)/$(SPECIES2).aligned.blastall_fa,$<,$@)
	@touch $@
	@touch $@.aln_finished

A2U2_RES=$(DIR_P_OLDNEW2)/blastall_res.concat_res
$(A2U2_RES): $(A2U2_RES_FILES)
	@echo "	Concatenating blastall results for $(SPECIES2) Unaligned vs Aligned"
	@find $(DIR_P_OLDNEW2) -name "$(SPECIES2).unaligned.*.blastall_res" | xargs cat > $@
	@touch $@
	@echo "	Finished concatenating blastall results for $(SPECIES2) Unaligned vs Aligned"

#
#	Aligned vs Aligned old results
#
A2A2_RES=$(DIR_P_OLDOLD2)/matching_from_prev.blastall_res
$(A2A2_RES): $(DIR_P_SEQ2)/$(SPECIES2).aligned.ids
	@echo "	Filtering previous blastall results matching current $(SPECIES2)" 
	@$(DIR_BIN)/seq_pairs_filter_by_identifier \
        --matching_identifiers $< \
        --input_file $(DIR_ALIGNED2)/curr.blastall_results \
        --results_summary $(DIR_P_SUMMARY)/prev_blastall_results.$(SPECIES2).counts \
        --both $@
	@echo "	Finished filtering previous blastall results matching current $(SPECIES2)" 

#
#	Concatenate results together
#		save first to tmp file, rename old file, rename tmp file
# 
#$(DIR_ALIGNED2)/curr.blastall_results: $(A2A2_RES) $(U2U2_RES) $(A2U2_RES)
#	@echo "	Concatenating blastall results for $(SPECIES2) against itself"
#	@# save sequences/cdna.ids to align_results/cdna.ids with original timestamp
#	@cp -p $(DIR_SEQ2)/cdna.ids $(DIR_ALIGNED2)/curr.cdna.ids.tmp
#	@mv $(DIR_ALIGNED2)/curr.cdna.ids $(DIR_ALIGNED2)/cdna.ids.prev
#	@mv $(DIR_ALIGNED2)/curr.cdna.ids.tmp $(DIR_ALIGNED2)/curr.cdna.ids
#	@# make sure directory is writeable
#	@cat $^ > $@.tmp
#	@# create prev file if necessary and backup to .prev
#	$(call create_unless_exists,$(DIR_ALIGNED2)/blastall_results.prev)
#	@find $(DIR_ALIGNED2) -name "*.prev" -exec mv '{}' '{}'.prev ';'
#	@mv $(DIR_ALIGNED2)/curr.blastall_results $(DIR_ALIGNED2)/blastall_results.prev
#	@# concatenate files
#	@mv $@.tmp $@
#	@date > $(DIR_P_SUMMARY)/blast.$(SPECIES2).blast_finish.date
#	@echo "	Finished concatenating blastall results for $(SPECIES2) against itself"


#
#	Concatenate results together
# 
$(DIR_P_BLAST)/$(SPECIES2).concatenated_blastall_results:  $(A2A2_RES) $(U2U2_RES) $(A2U2_RES)
	@echo "	Concatenating blastall results for $(SPECIES2) against itself"
	@cat $^ > $@
	@echo "	Finished concatenating blastall results for $(SPECIES2) against itself"

#
##########################################################################################



##########################################################################################
#
#   Species 1 vs Species 2 blastall
#
#	Unaligned vs Unaligned sequences
#
#	U1U2_RES
#	$(DIR_P_NEWNEW12)
#	$(SPECIES12).unaligned.%.blastall_res
U1U2_RES_FILES=$(UNALIGNED1_FA:$(DIR_P_SEQ1)/$(SPECIES1).unaligned.%.fa=$(DIR_P_NEWNEW12)/$(SPECIES12).unaligned.%.blastall_res)
$(DIR_P_NEWNEW12)/$(SPECIES12).unaligned.%.blastall_res: \
		$(DIR_P_SEQ1)/$(SPECIES1).unaligned.%.fa \
		$(DIR_P_SEQ2)/$(SPECIES2).unaligned.blastall_fa
	$(call blastall_if_non_zero,U1U2_$(*F)_$(SPECIES12),$(DIR_P_SEQ2)/$(SPECIES2).unaligned.blastall_fa,$<,$@)
	@touch $@
	@touch $@.aln_finished

U1U2_RES=$(DIR_P_NEWNEW12)/blastall_res.concat_res
$(U1U2_RES): $(U1U2_RES_FILES)
	@echo "	Concatenating blastall results for $(SPECIES1) Unaligned vs $(SPECIES2) Unaligned"
	@find $(DIR_P_NEWNEW12) -name "*.blastall_res" | xargs cat > $@
	@touch $@
	@echo "	Finished concatenating blastall results for $(SPECIES1) Unaligned vs $(SPECIES2) Unaligned"

#
#	Unaligned 1 vs Aligned 2
#
U1A2_RES_FILES=$(UNALIGNED1_FA:$(DIR_P_SEQ1)/$(SPECIES1).unaligned.%.fa=$(DIR_P_NEW1OLD2)/$(SPECIES12).unaligned.%.blastall_res)
$(DIR_P_NEW1OLD2)/$(SPECIES12).unaligned.%.blastall_res: \
                    $(DIR_P_SEQ1)/$(SPECIES1).unaligned.%.fa \
		    $(DIR_P_SEQ2)/$(SPECIES2).aligned.blastall_fa
	$(call blastall_if_non_zero,U1A2_$(*F)_$(SPECIES12),$(DIR_P_SEQ2)/$(SPECIES2).aligned.blastall_fa,$<,$@)
	@touch $@
	@touch $@.aln_finished

U1A2_RES=$(DIR_P_NEW1OLD2)/blastall_res.concat_res
$(U1A2_RES): $(U1A2_RES_FILES)
	@echo "	Concatenating blastall results for $(SPECIES1) Unaligned vs $(SPECIES2) aligned"
	@find $(DIR_P_NEW1OLD2) -name "*.blastall_res" | xargs cat > $@
	@touch $@
	@echo "	Finished concatenating blastall results for $(SPECIES1) Unaligned vs $(SPECIES2) aligned"

#
#	Unaligned 2 vs Aligned 1
#
U2A1_RES_FILES=$(UNALIGNED2_FA:$(DIR_P_SEQ2)/$(SPECIES2).unaligned.%.fa=$(DIR_P_NEW2OLD1)/$(SPECIES12).unaligned.%.blastall_res)
$(DIR_P_NEW2OLD1)/$(SPECIES12).unaligned.%.blastall_res: \
                    $(DIR_P_SEQ2)/$(SPECIES2).unaligned.%.fa \
                    $(DIR_P_SEQ1)/$(SPECIES1).aligned.blastall_fa
	$(call blastall_if_non_zero,U2A1_$(*F)_$(SPECIES12),$(DIR_P_SEQ1)/$(SPECIES1).aligned.blastall_fa,$<,$@)
	@touch $@
	@touch $@.aln_finished

U2A1_RES=$(DIR_P_NEW2OLD1)/blastall_res.concat_res
$(U2A1_RES): $(U2A1_RES_FILES)
	@echo "	Concatenating blastall results for $(SPECIES2) Unaligned vs $(SPECIES1) aligned"
	@find $(DIR_P_NEW2OLD1) -name "*.blastall_res" | xargs cat > $@
	@touch $@
	@echo "	Finished concatenating blastall results for $(SPECIES2) Unaligned vs $(SPECIES1) aligned"



#
#	Aligned vs Aligned old results
#
#		DIR_P_OLD1OLD2
#
A1A2_IDS=$(DIR_P_OLD1OLD2)/$(SPECIES12).ids
$(A1A2_IDS): $(DIR_P_SEQ1)/$(SPECIES1).aligned.ids $(DIR_P_SEQ2)/$(SPECIES2).aligned.ids
	@echo "	List of all aligned identifiers"
	@cat $^ > $@

	
A1A2_RES=$(DIR_P_OLD1OLD2)/matching_from_prev.blastall_res
$(A1A2_RES): $(A1A2_IDS)
	@echo "	Filtering previous blastall results matching current $(SPECIES1) and $(SPECIES2)" 
	@$(DIR_BIN)/seq_pairs_filter_by_identifier \
        --matching_identifiers $< \
        --input_file $(DIR_ALIGNED12)/curr.blastall_results \
        --results_summary $(DIR_P_SUMMARY)/prev_blastall_results.$(SPECIES12).counts \
        --both $@
	@echo "	Finished filtering previous blastall results matching current $(SPECIES1) and $(SPECIES2)" 


#
#	Concatenate results together
# 
$(DIR_P_BLAST)/$(SPECIES12).concatenated_blastall_results:  $(U1U2_RES) $(U1A2_RES) $(U2A1_RES) $(A1A2_RES)
	@echo "	Concatenating blastall results for $(SPECIES1) vs $(SPECIES2)"
	@cat $^ > $@
	@echo "	Finished concatenating blastall results for $(SPECIES1) vs $(SPECIES2)"


#
##########################################################################################






